<?php
/**
 *   駿濤 （xjt@ourstu.com）
 */
namespace Pingxx\Model;

use Think\Model;


class PingxxChargeModel extends Model
{
    protected $tableName = 'pingxx_charge';

    public function addCharge($charge)
    {

        $charge['amount'] = $charge['amount'] / 100;
        $data['charge_id'] = $charge['id'];
        $data['channel'] = $charge['channel'];
        $data['paid'] = $charge['paid'];
        $data['time_paid'] = $charge['time_paid'];
        $data['order_no'] = $charge['order_no'];
        $data['data'] = json_encode($charge);
        $data['created'] = $charge['created'];
        $data['amount'] = $charge['amount'];
        $data['buyer_account'] = $charge['extra']['buyer_account']?$charge['extra']['buyer_account']:$charge['extra']['open_id'];
        $result = $this->add($data);
        if (!$result) {
            return false;
        }
        return $data['order_no'];
    }


    public function getCharge($charge_id)
    {
        $charge = S('pingxx_charge_' . $charge_id);
        if (empty($charge)) {
            $charge = $this->where(array('charge_id' => $charge_id))->find();
            $data = json_decode($charge['data'], true);
            $charge['data'] = $data;
            S('pingxx_charge_' . $charge_id, $charge, 60 * 60);
        }
        return $charge;
    }


    public function getChargeStatus($orderNo)
    {
        $charge = $this->where(array('order_no' => $orderNo))->find();
        return $charge['paid'];
    }

}